GBML manual / Tweens

Tweens

A tween glides a number from one value to another over a number of frames - a menu sliding in, a door opening, a bar filling. Start here - it says which of the two ways to make one to use (to make something move, it is tween_to), lists all sixteen curves, and says what happens to a glide when its instance is destroyed.

A number that takes its time

Setting x = 200 puts something at 200 in one frame. A tween is the number getting there over time: it starts where it is and arrives at 200 after, say, a second. How it travels is the curve: steady, speeding up, or braking at the end.

Want something to move? Use tween_to (it glides a variable from where it is now) or tween_from_to (it sets the start for you first). Both begin right now and run by themselves. tween is the other way: it only works out a number for you to use yourself.

So there are two ways to make one, and they suit different jobs.

tweentween_to
What it isA number you read.A glide the engine runs for you.
Where it livesNowhere. It is worked out from the frame counter each time.In the engine, until it arrives.
You writey = tween(0, 80, start, 120, EASE_QUAD_OUT); every frame.tween_to(y, 80, 120, EASE_QUAD_OUT); once.
Good forA value you draw or compare, like a bar or a fade.Moving an instance or changing one of its variables.
In a pauseKeeps counting.Waits.

Moving a variable for you

Write the name of the variable, where it should end up, how many frames it takes and the curve:

tween_to(x, x + 64, 60, EASE_QUAD_OUT);

The walker's x glides 64 pixels to the right over one second, fast at first and braking as it arrives. Nothing else is needed: every frame the engine moves it a little further, and when it arrives it stops.

It works on any variable of an instance that holds a number: the built-in ones (x, y, angle, xscale...) and the ones you made yourself (hp, glow...). Name another instance's variable the same way you would read it:

var boss = instance_nearest(x, y, obj_doc_boss);
if (boss != nothing) {
    tween_to(boss.hp, 0, 120, EASE_LINEAR);
}

The boss's hp runs down to 0 over two seconds. A whole-number variable gets the nearest whole number on every step. A local variable or a global cannot be tweened, because they do not belong to an instance; the checker says so. In a script, x and the other built-in variables are the caller's, so tween_to(x, 100, 30, EASE_LINEAR) moves whoever called the script.

tween_from_to sets the start for you instead of using where the variable is now. tween_stop stops a glide where it has got to. tween_running says whether one is still going, so a move can wait for the one before it:

if (!tween_running(x)) {
    if (x < xstart + 1) { tween_to(x, xstart + 64, 60, EASE_SINE_IN_OUT); }
    else { tween_to(x, xstart, 60, EASE_SINE_IN_OUT); }
}

Starting a tween on a variable that already has one replaces it: the new glide starts from where the variable is now, and the two never fight over the value.

The sixteen curves

Every tween call asks for a curve, and these are the sixteen you can pick from. Each name is a constant you can click to read about it (they are also listed together under Constants in the Reference).

Starts slowly, speeds up (in)Starts fast, brakes (out)Slow at both ends (in out)
SteadyEASE_LINEAR
GentleEASE_SINE_INEASE_SINE_OUTEASE_SINE_IN_OUT
StrongerEASE_QUAD_INEASE_QUAD_OUTEASE_QUAD_IN_OUT
Winds up, runs past (goes outside the range)EASE_BACK_INEASE_BACK_OUTEASE_BACK_IN_OUT
Bounces (stays inside the range)EASE_BOUNCE_INEASE_BOUNCE_OUTEASE_BOUNCE_IN_OUT
Springs (goes outside the range)EASE_ELASTIC_INEASE_ELASTIC_OUTEASE_ELASTIC_IN_OUT

The tween page shows how far along each one is after a quarter, half and three quarters of the time, and what is left out (cubic and steeper, circ and expo).

Curves that go outside the range

The first seven curves always keep the number between where it starts and where it ends. Back and elastic do not. They go a little (back) or a lot (elastic) past the end, or behind the start, because that is what makes a move feel springy, and then they settle exactly on the end. That is wonderful for a position and a mistake for a number with limits: if you tween hp or a volume with one of them, it will go above full or below empty for a moment. Use sine, quad or bounce for those. A bounce is the third family and it is the safe one: it never leaves the range.

Here are the three families, side by side. In each picture a ball moves steadily to the right (that is only the time, so you can see the shape) and its height is the number: it starts at the dim line and ends on the green one. Look for the ball going past the green line.

A real capture, 27 frames played in a loop - three small graphs, one above the other, labeled in, out and in out. In each one a ball moves steadily to the right while its height on the screen is the number being tweened: a dim dashed line at the top is where the number starts, a green dashed line below it is where it ends, and a trail of dots shows where the ball has been. In the out row the ball sinks a little past the green line and then comes back up onto it. In the in row it first rises a little above the dim line, the wrong way, and then sinks down to the green line. The in out row does both. The first frame, which is all a viewer that cannot animate shows, is the finished picture: every dot, drawing the whole of each curve.A real capture, 27 frames played in a loop - three small graphs, one above the other, labeled in, out and in out. In each one a ball moves steadily to the right while its height on the screen is the number being tweened: a dim dashed line at the top is where the number starts, a green dashed line below it is where it ends, and a trail of dots shows where the ball has been. In the out row the ball sinks a little past the green line and then comes back up onto it. In the in row it first rises a little above the dim line, the wrong way, and then sinks down to the green line. The in out row does both. The first frame, which is all a viewer that cannot animate shows, is the finished picture: every dot, drawing the whole of each curve.
A real capture, 27 frames played in a loop - three small graphs, one above the other, labeled in, out and in out. In each one a ball moves steadily to the right while its height on the screen is the number being tweened: a dim dashed line at the top is where the number starts, a green dashed line below it is where it ends, and a trail of dots shows where the ball has been. In the out row the ball sinks a little past the green line and then comes back up onto it. In the in row it first rises a little above the dim line, the wrong way, and then sinks down to the green line. The in out row does both. The first frame, which is all a viewer that cannot animate shows, is the finished picture: every dot, drawing the whole of each curve.

The same tween three ways, one row for each of EASE_BACK_IN, EASE_BACK_OUT and EASE_BACK_IN_OUT. The ball's height is the number (like tween_to(y, ...) moving something down), and it drifts right at a steady speed only so that the shape can be seen. The dim line is where the number starts and the green line is where it ends: look for the ball going past the green line, or behind the dim one. Real pictures from a Game Boy Advance ROM, 4 frames apart, played at the speed of the console. A viewer that does not animate shows the first picture, the finished one.

A real capture, 27 frames played in a loop - three small graphs, one above the other, labeled in, out and in out. In each one a ball moves steadily to the right while its height on the screen is the number being tweened: a dim dashed line at the top is where the number starts, a green dashed line below it is where it ends, and a trail of dots shows where the ball has been. In the out row the ball drops to the green line like a dropped ball and bounces back up three times, each bounce smaller than the last, before it rests on the line. The in row is the same thing the other way round: small hops off the dim line that get taller, and then a drop to the green line. The in out row bounces away from the start and then into the end. No ball ever goes below the green line or above the dim one. The first frame, which is all a viewer that cannot animate shows, is the finished picture: every dot, drawing the whole of each curve.A real capture, 27 frames played in a loop - three small graphs, one above the other, labeled in, out and in out. In each one a ball moves steadily to the right while its height on the screen is the number being tweened: a dim dashed line at the top is where the number starts, a green dashed line below it is where it ends, and a trail of dots shows where the ball has been. In the out row the ball drops to the green line like a dropped ball and bounces back up three times, each bounce smaller than the last, before it rests on the line. The in row is the same thing the other way round: small hops off the dim line that get taller, and then a drop to the green line. The in out row bounces away from the start and then into the end. No ball ever goes below the green line or above the dim one. The first frame, which is all a viewer that cannot animate shows, is the finished picture: every dot, drawing the whole of each curve.
A real capture, 27 frames played in a loop - three small graphs, one above the other, labeled in, out and in out. In each one a ball moves steadily to the right while its height on the screen is the number being tweened: a dim dashed line at the top is where the number starts, a green dashed line below it is where it ends, and a trail of dots shows where the ball has been. In the out row the ball drops to the green line like a dropped ball and bounces back up three times, each bounce smaller than the last, before it rests on the line. The in row is the same thing the other way round: small hops off the dim line that get taller, and then a drop to the green line. The in out row bounces away from the start and then into the end. No ball ever goes below the green line or above the dim one. The first frame, which is all a viewer that cannot animate shows, is the finished picture: every dot, drawing the whole of each curve.

The same tween three ways, one row for each of EASE_BOUNCE_IN, EASE_BOUNCE_OUT and EASE_BOUNCE_IN_OUT. The ball's height is the number (like tween_to(y, ...) moving something down), and it drifts right at a steady speed only so that the shape can be seen. The dim line is where the number starts and the green line is where it ends: the ball touches the green line but never goes below it. Real pictures from a Game Boy Advance ROM, 4 frames apart, played at the speed of the console. A viewer that does not animate shows the first picture, the finished one.

A real capture, 27 frames played in a loop - three small graphs, one above the other, labeled in, out and in out. In each one a ball moves steadily to the right while its height on the screen is the number being tweened: a dim dashed line at the top is where the number starts, a green dashed line below it is where it ends, and a trail of dots shows where the ball has been. In the out row the ball shoots down past the green line by a long way and then wobbles up and down round it with smaller and smaller swings until it rests on it, like a spring let go. The in row is the same wobble the other way round: swings round the dim line that grow bigger, and then a drop onto the green line. The in out row winds up round the dim line, crosses the middle, and wobbles round the green line. The first frame, which is all a viewer that cannot animate shows, is the finished picture: every dot, drawing the whole of each curve.A real capture, 27 frames played in a loop - three small graphs, one above the other, labeled in, out and in out. In each one a ball moves steadily to the right while its height on the screen is the number being tweened: a dim dashed line at the top is where the number starts, a green dashed line below it is where it ends, and a trail of dots shows where the ball has been. In the out row the ball shoots down past the green line by a long way and then wobbles up and down round it with smaller and smaller swings until it rests on it, like a spring let go. The in row is the same wobble the other way round: swings round the dim line that grow bigger, and then a drop onto the green line. The in out row winds up round the dim line, crosses the middle, and wobbles round the green line. The first frame, which is all a viewer that cannot animate shows, is the finished picture: every dot, drawing the whole of each curve.
A real capture, 27 frames played in a loop - three small graphs, one above the other, labeled in, out and in out. In each one a ball moves steadily to the right while its height on the screen is the number being tweened: a dim dashed line at the top is where the number starts, a green dashed line below it is where it ends, and a trail of dots shows where the ball has been. In the out row the ball shoots down past the green line by a long way and then wobbles up and down round it with smaller and smaller swings until it rests on it, like a spring let go. The in row is the same wobble the other way round: swings round the dim line that grow bigger, and then a drop onto the green line. The in out row winds up round the dim line, crosses the middle, and wobbles round the green line. The first frame, which is all a viewer that cannot animate shows, is the finished picture: every dot, drawing the whole of each curve.

The same tween three ways, one row for each of EASE_ELASTIC_IN, EASE_ELASTIC_OUT and EASE_ELASTIC_IN_OUT. The ball's height is the number (like tween_to(y, ...) moving something down), and it drifts right at a steady speed only so that the shape can be seen. The dim line is where the number starts and the green line is where it ends: the ball goes a long way past the green line (and, for the in, behind the dim one). Real pictures from a Game Boy Advance ROM, 4 frames apart, played at the speed of the console. A viewer that does not animate shows the first picture, the finished one.

When the instance is destroyed

A glide only ever writes into the instance it was started on. If that instance is destroyed, the glide just ends. It does not write into the dead instance, and it does not write into a new instance that was created in the same place a moment later: a tween remembers exactly which instance it was started on, not just where it lived. The game goes on as if nothing happened.

When you change room, every instance that is not persistent is destroyed, so their glides end too. A persistent instance keeps gliding into the next room.

At most 16 at once, or the number you set

Only 16 glides can run at the same moment (tween_max) - unless you changed Tweens gliding at once under Engine limits in Project Settings, from 1 to 64 (the rest of this page says 16, the number every game starts with); tween_count says how many are running now. The debug overlay has a TWEENS row with the same two numbers and the most there have ever been. If all 16 are busy, a new tween_to cannot glide: the variable jumps straight to its end value and tween_to gives back false. The game carries on and the door still ends up open, it just pops instead of sliding.

A game that never uses them

A game that never calls tween_to and its friends has none of this in its ROM: no table, no per-frame work. The reading form tween costs only where it is called.

The curves cost something too, but only the ones you use. A game that starts its tweens with tween_to and tween_from_to and always writes the curve out (EASE_QUAD_OUT) is built with just those curves. A curve held in a variable, or handed to a script, is one the game cannot know about in advance, so then it carries all sixteen. The reading form works out its curve where you call it and is not part of this.

See also: tweentween_totween_from_totween_stoptween_runningtween_counttween_maxwavelerpEASE_LINEAREASE_BOUNCE_OUTEASE_BACK_OUTEASE_ELASTIC_OUT